package com.ityueqiangu.system.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.ityueqiangu.core.enums.CommonEnum;
import com.ityueqiangu.core.enums.SysFlagStatusEnum;
import com.ityueqiangu.core.exception.BizException;
import com.ityueqiangu.core.web.result.ResultDataUtil;
import com.ityueqiangu.core.web.result.ResultInfo;
import com.ityueqiangu.core.util.UserUtil;
import com.ityueqiangu.core.web.vo.ActiverUser;
import com.ityueqiangu.core.web.vo.Menu;
import com.ityueqiangu.system.domain.SysUser;
import com.ityueqiangu.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author BestClever
 * @title: IndexController
 * @projectName book-manage
 * @description: TODO
 * @date 2020-11-01 11:12
 */
@Controller
public class IndexController {

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 跳转到主页登录页面
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String toLogin(HttpServletRequest request){
        return "login";
    }

    /**
     * 跳转到主页登录页面
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo login(SysUser sysUser, HttpServletRequest request){
        ActiverUser activerUser = new ActiverUser();
        //执行登录逻辑
        sysUser.setLoginName(sysUser.getUserName());
        SysUser sysUserResult = sysUserService.selectSysUserByLoginName(sysUser);
        if (ObjectUtil.isNull(sysUserResult)) {
            throw new BizException(CommonEnum.ACCOUNT_NOT_EXIST);
        }
        BeanUtils.copyProperties(sysUserResult,activerUser);
        if(!StrUtil.equals(sysUserResult.getPassword(),sysUser.getPassword())){
            throw new BizException(CommonEnum.PASSWORD_ERROR);
        }
        if (StrUtil.equals(activerUser.getIsDeleted(), SysFlagStatusEnum.ONE.getKey())) {
            throw new BizException(CommonEnum.ACCOUNT_DISABLED);
        }
        request.getSession().setAttribute("loginUser", activerUser);
        return ResultDataUtil.createSuccess(CommonEnum.LOGIN_SUCCESS);
    }

    /**
     * 跳转到主页
     * @return
     */
    @RequestMapping(value = "/index")
    public String index(HttpServletRequest request){
        return "index";
    }

    /**
     * 跳转到主页
     * @return
     */
    @RequestMapping(value = "/personalInformation")
    public String personalInformation(HttpServletRequest request){
        return "personalInformation";
    }

    /**
     * 退出
     * @return
     */
    @RequestMapping(value = "/logout")
    public String logout(HttpServletRequest request){
        //删除session
        request.getSession().removeAttribute("loginUser");
        return "redirect:login";
    }

    /**
     * 欢迎页
     * @return
     */
    @RequestMapping(value = "/main")
    public String main(){
        return "main";
    }

    @RequestMapping(value = "/register")
    public String register(){
        return "register";
    }

    /**
     * 获取菜单
     *
     */
    @RequestMapping(value = "/nav")
    @ResponseBody
    public List<Menu> getNav(HttpServletRequest request){
        ArrayList<Menu> list = Lists.newArrayList();
        ActiverUser loginUser = UserUtil.getCurrentUser(request);
        Menu yhgl = new Menu();
        yhgl.setId(8);
        yhgl.setTitle("用户管理");
        yhgl.setIcon("&#xe653;");
        yhgl.setHref("sysUser/index");
        yhgl.setSpread(false);

        Menu lxkf = new Menu();
        lxkf.setId(9);
        lxkf.setTitle("联系客服");
        lxkf.setIcon("&#xe606;");
        lxkf.setHref("/sys/im/dialog");
        lxkf.setSpread(false);
        if(loginUser.getRoleId()==1){
            list.add(yhgl);
        }
        list.add(lxkf);
        return list;
    }
}
